package com.mida.bms.report.util;

import com.djhu.comms.filecache.FileCacheService;
import org.apache.commons.io.FilenameUtils;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @Description TODO
 * @Author K.L
 * @Date 2019/12/3
 **/
public class BaseUtils {

    /**
     * 删除文件夹下文件
     * @param dir
     */
    public static void deleteFiles(File dir) {
        if (dir.exists() && dir.isDirectory()) {
            String[] childrens = dir.list();
            for (String child : childrens) {
                File file1 = new File(dir, child);
                file1.delete();
            }
        }
    }

    /**
     * 获取当前时间
     *
     * @return
     */
    public static String getNow() {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String now = sdf.format(date);
        return now;
    }

    /**
     * 获取文件存储根目录到年月日
     * @return
     */
//    public static String getRootDir(){
//        final Calendar now = Calendar.getInstance();
//        final File yearDir = new File(Const.rootPath, String.valueOf(now.get(Calendar.YEAR)));
//        StringBuffer itemDirName = new StringBuffer();
//        itemDirName.append(new SimpleDateFormat("MMdd").format(new Date()));
//        final File nowDir = new File(yearDir, itemDirName.toString());
//        if (!nowDir.exists()) {
//            nowDir.mkdirs();
//        }
//        return nowDir.getAbsolutePath();
//    }

    /**
     * 获取序列级别绝对路径
     * @param studyUid
     * @param seriesUid
     * @return
     */
//    public static String getSeriesAbsolutePath(String studyUid, String seriesUid){
//        File studyDir = new File(getRootDir(), studyUid);
//        File seriesDir = new File(studyDir, seriesUid);
//        if (!seriesDir.exists()) {
//            seriesDir.mkdirs();
//        }
//        return seriesDir.getAbsolutePath();
//    }

    /**
     * 获取删除日期
     * @param day
     * @return
     */
    public static String getExpireDate(Integer day){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.DAY_OF_MONTH, -day);
        Date m = c.getTime();
        String date = sdf.format(m);
        return date;
    }

    /**
     * 获取延迟后推送时间
     * @param minute
     * @return
     */
    public static String getDelayDate(Integer minute){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.MINUTE, -minute);
        Date m = c.getTime();
        String date = sdf.format(m);
        return date;
    }

    /**
     * 获取小数位为3位的数值差
     * @param start
     * @param end
     * @return
     */
    public static BigDecimal getUseTime(long start, long end){
        float use = (end-start) / 1000F;
        DecimalFormat decimalFormat=new DecimalFormat("0.000");
        BigDecimal b = new BigDecimal(decimalFormat.format(use));
        return b;
    }

    public static void delFiles(String seriesPath, FileCacheService fileCacheConfigService) throws IOException {
        fileCacheConfigService.getCurrentCache().deleteFilesByDir(FilenameUtils.normalize(seriesPath.concat("/")));
    }

    public static void main(String[] args){
    }

}
